package com.zilue.hfqueue.context;

import com.zilue.hfqueue.HFQueueConfig;
import com.zilue.hfqueue.queue.HFQueue;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ZD on 2017/5/9.
 */
public class HFQueueContext {
    private static ConcurrentHashMap<String, HFQueue> QueueMap;

    static {
        QueueMap = new ConcurrentHashMap<>();
    }

    public void init() {
    }

    public static HFQueue getQueue(String topic) throws IOException {
        HFQueue hfQueue = QueueMap.get(topic);
        if (hfQueue != null)
            return hfQueue;

        if (HFQueueConfig.getAutoCreateWhenGet()) {
            HFQueue queue = HFQueue.builder().topic(topic).build();
            QueueMap.put(topic, queue);
            return queue;
        }
        return null;
    }
}
